using AutoMapper;
using net_work.Common;
using net_work.Common.Request;
using net_work.Data.VO.Cou;
using net_work.IRepository.Cou;
using net_work.IRepository.Sys;
using net_work.IService.Cou;

namespace net_work.Service.Cou;

public class CurriculumService(
    IMapper mapper,
    IUserContext userContext,
    ITeacherRepository teacherRepository,
    IStudentRepository studentRepository,
    IUserRepository userRepository,
    ITeacherTeachingRepository teacherTeachingRepository,
    ITeachingRepository teachingRepository,
    IEnrollmentRepository enrollmentRepository
) : ICurriculumService
{
    public Task<List<List<CurriculumResult>>?> GetCurriculumAsync(int weekNum)
    {
        var role = userContext.UserRole;
        return role == "STUDENT" ? GetCurriculumByStudentAsync(weekNum) : GetCurriculumByTeacherAsync(weekNum);
    }

    private async Task<List<List<CurriculumResult>>?> GetCurriculumByStudentAsync(int weekNum)
    {
        // 初始化结果列表，长度为 7，对应周一到周日
        var results = new List<List<CurriculumResult>>();
        for (var i = 0; i < 7; i++)
        {
            results.Add([]);
        }

        // 获取学生信息
        var student = await studentRepository.GetStudentPagedAsync(new PagedRequest(1, PageSizeEnum.Unlimited,
            [new FilterCondition("UserId", FilterOperator.Equals, userContext.UserId)]));
        if (student.TotalRecords <= 0)
        {
            return results;
        }

        var studentId = student.Data.First().Id;

        // 获取学生已选课程安排
        var enrollmentPagedResult = await enrollmentRepository.GetPagedAsync(new PagedRequest(1, PageSizeEnum.Unlimited,
            [new FilterCondition("StudentId", FilterOperator.Equals, studentId)]));
        var enrollmentTeachIds = enrollmentPagedResult.Data.Select(x => x.TeachId).ToList();

        if (enrollmentTeachIds.Count == 0)
        {
            return results; // 如果没有选课记录，返回空的二维数组
        }

        var teaching = await teachingRepository.GetTeachingPagedAsync(new PagedRequest(1, PageSizeEnum.Unlimited,
            [new FilterCondition("Id", FilterOperator.In, enrollmentTeachIds)]));

        var teachingResults = teaching.Data.Where(x => x.TeachWeekLength >= weekNum);

        foreach (var teachingResult in teachingResults)
        {
            if (teachingResult.ScheduleInfoList == null)
            {
                continue;
            }

            foreach (var scheduleInfo in teachingResult.ScheduleInfoList)
            {
                if ((int)scheduleInfo.WeekDay < 1 || (int)scheduleInfo.WeekDay > 7)
                {
                    continue;
                }

                var newCurriculumResult = mapper.Map<CurriculumResult>(teachingResult);
                newCurriculumResult.ScheduleInfo = scheduleInfo;
                results[(int)scheduleInfo.WeekDay - 1].Add(newCurriculumResult);
            }
        }

        return results;
    }

    private async Task<List<List<CurriculumResult>>?> GetCurriculumByTeacherAsync(int weekNum)
    {
        // 初始化结果列表，长度为 7，对应周一到周日
        var results = new List<List<CurriculumResult>>();
        for (var i = 0; i < 7; i++)
        {
            results.Add([]);
        }
        
        var teacher = await teacherRepository.GetTeacherPagedAsync(new PagedRequest(1, PageSizeEnum.Unlimited,
            [new FilterCondition("UserId", FilterOperator.Equals, userContext.UserId)]));
        if (teacher.TotalRecords <= 0)
        {
            return results;
        }

        var teacherId = teacher.Data.First().Id;

        var teacherTeaching = await teacherTeachingRepository.GetPagedAsync(new PagedRequest(1, PageSizeEnum.Unlimited,
            [new FilterCondition("TeacherId", FilterOperator.Equals, teacherId)]));

        var teachingIds = teacherTeaching.Data.Select(x => x.TeachId).ToList();

        if (teachingIds.Count == 0)
        {
            return results; // 如果没有授课记录，返回空的二维数组
        }

        var teaching = await teachingRepository.GetTeachingPagedAsync(new PagedRequest(1, PageSizeEnum.Unlimited,
            [new FilterCondition("Id", FilterOperator.In, teachingIds)]));

        var teachingResults = teaching.Data.Where(x => x.TeachWeekLength >= weekNum);

        foreach (var teachingResult in teachingResults)
        {
            if (teachingResult.ScheduleInfoList == null)
            {
                continue;
            }

            foreach (var scheduleInfo in teachingResult.ScheduleInfoList)
            {
                if ((int)scheduleInfo.WeekDay < 1 || (int)scheduleInfo.WeekDay > 7)
                {
                    continue;
                }

                var newCurriculumResult = mapper.Map<CurriculumResult>(teachingResult);
                newCurriculumResult.ScheduleInfo = scheduleInfo;
                results[(int)scheduleInfo.WeekDay - 1].Add(newCurriculumResult);
            }
        }

        return results;
    }
}